﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using DMS.Business.Modules.ArchivesManagement;
using DMS.Business.Modules.SystemManagement;
using DMS.Business.Utility;
using DMS.Data;

namespace DMS.Web.Common.Services
{
    public partial class BaseInfoFormHandler : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string methodName = Request["method"];
            MethodInfo method = GetType().GetMethod(methodName);

            method.Invoke(this, null);
        }

        public void ValidateValue()
        {
            bool isValidate = false;
            string validateId = Request.Params["validateId"];
            string validateValue = Request.Params["validateValue"];
            string[] param;
            switch (validateId)
            {
                case "UnitName":
                    param = validateValue.Split('|');
                    isValidate = BaseInfoService.IsUnitNameExist(param[0], param[1], param[2]);
                    break;
                case "TaxCode":
                    param = validateValue.Split('|');
                    isValidate = BaseInfoService.IsTaxCodeExist(param[0], param[1], param[2]);
                    break;
                case "InsuranceCode":
                    param = validateValue.Split('|');
                    isValidate = BaseInfoService.IsInsuranceCodeExist(param[0], param[1]);
                    break;
            }
            Response.Write(isValidate);
            Response.End();
        }

        /// <summary>
        /// 批量上传
        /// </summary>
        public void UploadBaseInfos()
        {
            string fileInfo = Request.Params["file"];
            string formType = Request.Params["formType"];
            string[] infos = fileInfo.Split(';');
            string localFilePath = infos[1];//ClientHelper.Instance.CopyFileToLocal(ConfigurationManager.AppSettings["FileServer"] + "/" + infos[1], string.Empty);
            //localFilePath = Server.MapPath(localFilePath);
            //string localFilePath = "H:\\workspace\\HPSB\\05.实现\\HPSB\\DMS.Web\\Attachments\\Temp\\基本信息导入模板-单位.xls";

            //如果数据检查有误，返回错误提示信息，返回错误内容、错误的行数、提示用户重新上传
            //如果数据检查无误，则进行导入临时表处理
            string result = BaseInfoTempService.ProcessBaseInfosExcel(localFilePath, formType);

            Response.Clear();
            Response.ContentType = "application/json";
            Response.Charset = "UTF-8";
            Response.Write(result);
            Response.End();
        }

        public void UpdateTempRow()
        {
            string infoId = Request.Params["infoId"];
            string updateProperty = Request.Params["updateProperty"];
            string updateValue = Request.Params["updateValue"];

            BaseInfoTempService.UpdateBaseInfoTemp(infoId, updateProperty, updateValue);
        }

        public void RemoveAllTemp()
        {
            string batchId = Request.Params["batchId"];
            BaseInfoTempService.RemoveByBatchId(batchId);
        }

        public void GetReasonList()
        {
            string statusCode = Request.Params["statusCode"];
            List<SYS_Enums> li = new EnumService().GetEnums(statusCode);
            string response = "<option value='' >请选择原因分类...</option >";
            foreach (var sysEnumse in li)
            {
                response += "<option value='" + sysEnumse.EnumID + "'>" + sysEnumse.EnumName + "</option>";
            }

            Response.Write(response);
            Response.End();
        }

        /// <summary>
        /// 返回输出流
        /// </summary>
        /// <param name="result"></param>
        public void WriteResponse(string result)
        {
            Response.Clear();
            Response.ContentType = "application/json";
            Response.Charset = "UTF-8";
            if (!String.IsNullOrEmpty(result))
                Response.Write(result);
            Response.End();
        }
        
        public void DestoryArchInfos()
        {
            try
            {
                string ids = Request["ids"];
                if(!string.IsNullOrEmpty(ids))
                {
                    string[] idArray = ids.Split(',');
                    string condition = " and it.ArchivesID in {";
                    int i = 0;
                    foreach (var id in idArray)
                    {
                        condition += "'" + id + "',";
                        i++;
                    }
                    if (i > 0)
                    {
                        condition = condition.Substring(0, condition.Length - 1) + "}";
                    }

                    ArchInfoService.DestroyArchInfo(condition);

                    Response.Write("ok");
                }else
                {
                    Response.Write("缺少参数！");
                }
               
            }
            catch (Exception ex)
            {
                LogHelper.LogException(ex);
                Response.Write(ex.Message);
            }finally
            {
                Response.End();
            }
        }

        /// <summary>
        /// 批量上传
        /// </summary>
        public void UploadArchInfos()
        {
            string fileInfo = Request.Params["file"];
            string baseInfoId = Request.Params["baseInfoId"];
            string[] infos = fileInfo.Split(';');
            string localFilePath = infos[1];
                //ClientHelper.Instance.CopyFileToLocal(ConfigurationManager.AppSettings["FileServer"] + "/" + infos[1], string.Empty);
            //localFilePath = Server.MapPath(localFilePath);
            //string localFilePath = "H:\\workspace\\HPSB\\05.实现\\HPSB\\DMS.Web\\Attachments\\Temp\\资料信息导入模板-全局.xls";

            //如果数据检查有误，返回错误提示信息，返回错误内容、错误的行数、提示用户重新上传
            //如果数据检查无误，则进行导入临时表处理
            string result = "";
            if (Request.Params["templateType"]!=null && Request.Params["templateType"].Equals("old"))
            {
                result = ArchInfoService.ProcessArchInfosExcelOld(localFilePath, baseInfoId);

            }
            else
            {
                result = ArchInfoService.ProcessArchInfosExcel(localFilePath, baseInfoId);
            }

            Response.Clear();
            Response.ContentType = "application/json";
            Response.Charset = "UTF-8";
            Response.Write(result);
            Response.End();
        }

        /// <summary>
        /// 批量上传
        /// </summary>
        public void UploadZHArchInfos()
        {
            string fileInfo = Request.Params["file"];
            string[] infos = fileInfo.Split(';');
            string localFilePath = infos[1];//ClientHelper.Instance.CopyFileToLocal(ConfigurationManager.AppSettings["FileServer"] + "/" + infos[1], string.Empty);
            //localFilePath = Server.MapPath(localFilePath);
            //string localFilePath = "H:\\workspace\\HPSB\\05.实现\\HPSB\\DMS.Web\\Attachments\\Temp\\综合资料信息导入模板.xls";

            //如果数据检查有误，返回错误提示信息，返回错误内容、错误的行数、提示用户重新上传
            //如果数据检查无误，则进行导入临时表处理
            string result = ArchInfoService.ProcessZHArchInfosExcel(localFilePath);

            Response.Clear();
            Response.ContentType = "application/json";
            Response.Charset = "UTF-8";
            Response.Write(result);
            Response.End();
        }
    }
}